package net.kosev.battleships;

import net.kosev.battleships.adapters.PlayerBoardAdapter;
import net.kosev.battleships.model.BattleshipsException;
import net.kosev.battleships.model.BoardException;
import net.kosev.battleships.model.Competitor;
import net.kosev.battleships.model.Game;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * Arrange ships activity
 * 
 * @author Nikola
 *
 */
public class ArrangeScreen extends Activity implements OnClickListener, OnItemSelectedListener {
	
	/**
	 * OK result
	 */
	public static final int RESULT_OK = 1;
	
	/**
	 * Cancel result
	 */
	public static final int RESULT_CANCEL = 0;
	
	/**
	 * Grid component
	 */
	private GridView mGrid = null;
	
	/**
	 * Adapter for the grid
	 */
	private PlayerBoardAdapter mAdapter = null;
	
	/**
	 * Reload button component
	 */
	private Button mReload = null;
	
	/**
	 * Play button component
	 */
	private Button mContinue = null;
	
	/**
	 * Computer difficulty spinner
	 */
	private Spinner mComputers = null;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.arrange);
		
		GameScreen.mGame = new Game();
		
		try {
			GameScreen.mGame.newGame();
		} catch (BoardException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		mGrid = (GridView) findViewById(R.id.Grid);
		mAdapter = new PlayerBoardAdapter(this, GameScreen.mGame.getPlayerBoard().getFields());
		mGrid.setAdapter(mAdapter);
		
		mReload = (Button) findViewById(R.id.ButtonReload);
		mReload.setOnClickListener(this);
		
		mContinue = (Button) findViewById(R.id.ButtonContinue);
		mContinue.setOnClickListener(this);
		
		mComputers = (Spinner) findViewById(R.id.SpinnerComputer);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.computers, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    mComputers.setAdapter(adapter);
	    mComputers.setSelection(Config.computerDifficulty);
	    mComputers.setOnItemSelectedListener(this);
	}

	/*
	 * (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ButtonReload:
			try {
				GameScreen.mGame.recreateBoard(Competitor.PLAYER);
				mAdapter = new PlayerBoardAdapter(this, GameScreen.mGame.getPlayerBoard().getFields());
				mGrid.setAdapter(mAdapter);
			} catch (BattleshipsException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case R.id.ButtonContinue:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			setResult(RESULT_CANCEL);
			finish();
	        return true;
	    }
		return super.onKeyDown(keyCode, event);
	}
	
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		Config.computerDifficulty = pos;
		Config.save(this);
	}

	public void onNothingSelected(AdapterView<?> arg0) {
	}

}
